package com.example.easycharge.notRepEntity;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.Objects;

@Data
public class ChargeRequest {
    @Schema(description = "充电请求序号")
    private int orderNumber;
    @Schema(description = "请求充电用户编号")
    private long customerId;
    @Schema(description = "请求充电的车辆编号")
    private long vehicleId;

    @Schema(description = "申请时剩余电量")
    private double originLeftEnergy;
    @Schema(description = "剩余电量千瓦时")
    private double leftEnergy;
    @Schema(description = "总电量")
    private double totalEnergy;
    @Schema(description = "请求充电电量")
    private double requestEnergy;

    @Schema(description = "最大接受充电功率（用户请求）")
    private int chargePower;
    @Schema(description = "实际充电功率")
    private int realChargePower;
    @Schema(description = "是否选择快充")
    private boolean fastCharge;

    @Schema(description = "预计充电时间")
    private int estimateChargeTime;
    @Schema(description = "预计等待时间")
    private int estimateWaitTime;
    @Schema(description = "电量百分比")
    private double batteryPercent;

    @Schema(description = "选择充电桩的编号，为0表示自动分配")
    private long selectPileId;
    @Schema(description = "车辆当前的坐标")
    private double latitude;
    @Schema(description = "车辆当前的坐标")
    private double longitude;

    @Schema(description = "遇到故障是否自动切换充电桩")
    private boolean autoFix;

    public ChargeRequest(long customerId, long vehicleId, int leftEnergy, int totalEnergy, long selectPileId, double latitude, double longitude, boolean autoFix) {
        this.customerId = customerId;
        this.vehicleId = vehicleId;
        this.leftEnergy = leftEnergy;
        this.totalEnergy = totalEnergy;
        this.selectPileId = selectPileId;
        this.latitude = latitude;
        this.longitude = longitude;
        this.autoFix = autoFix;
    }

    public ChargeRequest() {
    }

    public void increasePower() {
        // 每分钟充电一次
        leftEnergy += (double) realChargePower / 60;
        batteryPercent = leftEnergy / totalEnergy;
        estimateChargeTime--;
        if (leftEnergy > totalEnergy) {
            leftEnergy = totalEnergy;
        }
        if (batteryPercent > 100) {
            batteryPercent = 100;
        }
    }

    public void changeCapacity(NewCapacity newCapacity) {
        if (newCapacity.getRequestEnergy() + newCapacity.getLeftEnergy() > totalEnergy) {
            newCapacity.setRequestEnergy(totalEnergy - newCapacity.getLeftEnergy());
        }
        setLeftEnergy(newCapacity.getLeftEnergy());
        setRequestEnergy(newCapacity.getRequestEnergy());
        this.batteryPercent = leftEnergy / totalEnergy;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ChargeRequest that = (ChargeRequest) o;
        return customerId == that.customerId && vehicleId == that.vehicleId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(customerId, vehicleId);
    }
}
